package com.dingshang.dsapp.controller;

import com.dingshang.dsapp.dto.ResponseDTO;
import com.dingshang.dsapp.dto.ShejishiDTO;
import com.dingshang.dsapp.dto.WebInfoDTO;
import com.dingshang.dsapp.service.BoService;
import com.dingshang.dsapp.service.NavigationService;
import com.dingshang.dsapp.vo.WebInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

@Controller
public class PageController extends BaseController {

    @Autowired
    private NavigationService navigationService;

    @Autowired
    private BoService boService;

    @RequestMapping("/")
    public String toHome(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "index";
    }

    @RequestMapping("/bo")
    public String bo() {
        return "bo/login";
    }

    @RequestMapping("/bo/index.html")
    public String boIndex() {
        return "bo/index";
    }

    @RequestMapping("/bo/welcome.html")
    public String boWelcome() {
        return "bo/welcome";
    }

    @RequestMapping("/bo/toHomeBanner.html")
    public String toHomeBanner() {
        return "bo/homeBanner";
    }

    @RequestMapping("/toItemTwo.html")
    public String toItemTwo() {
        return "itemone";
    }

    @RequestMapping("/toEditor.html")
    public String toEditor() {
        return "editorTest";
    }

    @RequestMapping("/pic/{filename}")
    @ResponseBody
    public void showPic(@PathVariable("filename") String filename, HttpServletResponse response) {

        response.setContentType("image/png");
        try(OutputStream os = response.getOutputStream();
            
            InputStream is = new FileInputStream(filePath + filename)) {

            byte[] data = new byte[1024];
            int length = -1;
            while( -1 != (length = is.read(data))) {
                os.write(data, 0, length);
                os.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/upload")
    @ResponseBody
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) {

        if (file.isEmpty()) {
            System.out.println(">>>>>>>>>>upload() empty");
            return Collections.emptyMap();
        }

        String filename = System.currentTimeMillis() + "";

        Path path = Paths.get(filePath + filename);
        try {
            file.transferTo(path.toFile());
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(">>>>>>>>>>upload()" + file.getSize());

        List<String> list = new ArrayList<>(1);
        list.add(fileUrl + filename);

        Map<String, Object> map = new HashMap<>();
        map.put("errno", 0);
        map.put("data", list);

        return map;
    }

    @RequestMapping("/sheji.html")
    public String sheji(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "second";
    }

    @RequestMapping("/shejishi.html")
    public String shejishi(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "shejishi";
    }

    @RequestMapping("/bieshu.html")
    public String bieshu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "bieshu";
    }

    @RequestMapping("/pingcenggongyu.html")
    public String pingcenggongyu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "pingceng";
    }

    @RequestMapping("/sirendingzhi.html")
    public String sirendingzhi(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "shirendingzhi";
    }

    @RequestMapping("/meixueyanjiu.html")
    public String meixueyanjiu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "meixue";
    }

    @RequestMapping("/shejifuwu.html")
    public String shejifuwu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "shejifuwu";
    }

    @RequestMapping("/shigongfuwu.html")
    public String shigongfuwu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "shigongfuwu";
    }

    @RequestMapping("/shouhoufuwu.html")
    public String shouhoufuwu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "shouhoufuwu";
    }

    @RequestMapping("/shejisixiang.html")
    public String shejisixiang(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "shejisicun";
    }

    @RequestMapping("/zuixinhuodong.html")
    public String zuixinhuodong(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "zuixinhuodong";
    }

    @RequestMapping("/dingshanghuike.html")
    public String dingshanghuike(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "huike";
    }

    @RequestMapping("/women.html")
    public String women(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "women";
    }

    @RequestMapping("/pinpaigushi.html")
    public String pinpaigushi(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "women";
    }

    @RequestMapping("/qiyewenhua.html")
    public String qiyewenhua(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "women";
    }

    @RequestMapping("/hezuohuoban.html")
    public String hezuohuoban(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "women";
    }

    @RequestMapping("/lianxiwomen.html")
    public String lianxiwomen(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "women";
    }

    @RequestMapping("/zixun.html")
    public String zixun(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "shejisicun";
    }

    @RequestMapping("/fuwu.html")
    public String fuwu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "shejifuwu";
    }


    @RequestMapping("/bo/toWebInfo.html")
    public String toWebInfo(HttpServletRequest request) {
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "bo/boWebInfo";
    }

    @RequestMapping("/bo/saveWebInfo.html")
    @ResponseBody
    public ResponseDTO saveWebInfo(WebInfoVO vo) {
        boService.saveWebInfo(vo);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/toshejishi.html")
    public String toshejishi(HttpServletRequest request) {
        return "bo/boshejishi";
    }

    @RequestMapping("/bo/tobieshu.html")
    public String tobieshu(HttpServletRequest request) {
        return "bo/bobieshu";
    }

    @RequestMapping("/bo/tobieshuList.html")
    public String tobieshuList(HttpServletRequest request) {
        return "bo/boBieShuList";
    }

    @RequestMapping("/bo/toPingCeng.html")
    public String toPingCeng(HttpServletRequest request) {
        return "bo/boPingCeng";
    }

    @RequestMapping("/bo/toPingCengList.html")
    public String toPingCengList(HttpServletRequest request) {
        return "bo/boPingCengList";
    }

    @RequestMapping("/bo/toSiRen.html")
    public String toSiRen(HttpServletRequest request) {
        return "bo/boSiRen";
    }

    @RequestMapping("/bo/toSiRenList.html")
    public String toSiRenList(HttpServletRequest request) {
        return "bo/boSiRenList";
    }

    @RequestMapping("/bo/toMeiXue.html")
    public String toMeiXue(HttpServletRequest request) {
        return "bo/boMeiXue";
    }

    @RequestMapping("/bo/toMeiXueList.html")
    public String toMeiXueList(HttpServletRequest request) {
        return "bo/boMeiXueList";
    }

//    @RequestMapping("/bo/toSheJiFuWu.html")
//    public String toSheJiFuWu(HttpServletRequest request) {
//        request.setAttribute("shejifuwu", boService.getShejishi(9));
//        return "bo/boSheJiFuWu";
//    }

//    @RequestMapping("/bo/toShiGongFuWu.html")
//    public String toShiGongFuWu(HttpServletRequest request) {
//        request.setAttribute("shiGong", boService.getShejishi(10));
//        return "bo/boShiGongFuWu";
//    }

    @RequestMapping("/bo/saveshejishi.html")
    @ResponseBody
    public ResponseDTO saveshejishi(ShejishiDTO dto) {
        boService.saveShejishi(dto);
        return ResponseDTO.success();
    }

    @RequestMapping("/bo/toshejishiList.html")
    public String toshejishiList(HttpServletRequest request) {
        return "bo/boshejishiList";
    }

    @RequestMapping("/bo/shejishilist.html")
    @ResponseBody
    public ResponseDTO shejishilist(HttpServletRequest request, int valueType) {
        return ResponseDTO.success(boService.listShejishi(valueType));
    }

    @RequestMapping("/bo/deleteKeyValue.html")
    @ResponseBody
    public ResponseDTO deleteKeyValue(int id) {
        boService.deleteKeyValue(id);
        return ResponseDTO.success();
    }


    @RequestMapping("/bo/toAddAnli.html")
    public String toAddAnli(HttpServletRequest request) {
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "bo/boAnli";
    }

    @RequestMapping("/bo/toAnliList.html")
    public String toAnliList() {
        return "bo/boAnliList";
    }

    @RequestMapping("/bo/toService.html")
    public String toService(HttpServletRequest request) {
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "bo/boAddService";
    }

    @RequestMapping("/bo/toServerList.html")
    public String toServerList() {
        return "bo/boServerList";
    }

    @RequestMapping("/bo/yulan.html")
    public String yulan(int id, HttpServletRequest request) {
        request.setAttribute("yulan", boService.loadKv(id));
        return "bo/yulan";
    }

    @RequestMapping("/bo/toAddShejishi.html")
    public String toAddShejishi() {
        return "bo/boAddShejishi";
    }

    @RequestMapping("/bo/toShejishiList.html")
    public String toShejishiList() {
        return "bo/boshejishiList";
    }

    @RequestMapping("/bo/toGuanyu.html")
    public String toGuanyu() {
        return "bo/boAddGuanyu";
    }

    @RequestMapping("/bo/toGuanyuList.html")
    public String toGuanyuList() {
        return "bo/boAboutList";
    }

    @RequestMapping("/bo/toYusuan.html")
    public String toYusuan() {
        return "bo/boBaojiaList";
    }

    @RequestMapping("/bo/toPwd.html")
    public String toPwd() {
        return "bo/boPwd";
    }

    @RequestMapping("/bo/logout.html")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute("loginUser");
        request.getSession().invalidate();
        return "redirect:/bo";
    }

    @RequestMapping("/toXiandaisheji.html")
    public String toXiandaisheji(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "xiandaifengge";
    }

    @RequestMapping("/toXinzhongshifengge.html")
    public String toXinzhongshifengge(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "xinzhongshifengge";
    }

    @RequestMapping("/toMeishifengge.html")
    public String toMeishifengge(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "meishifengge";
    }

    @RequestMapping("/toOushifengge.html")
    public String toOushifengge(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "oushifengge";
    }

    @RequestMapping("/toShejiFuwu.html")
    public String toShejiFuwu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "shejifuwu";
    }

    @RequestMapping("/toShigongfuwu.html")
    public String toShigongfuwu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "shigongfuwu";
    }

    @RequestMapping("/toShouhoufuwu.html")
    public String toShouhoufuwu(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "shouhoufuwu";
    }

    @RequestMapping("/torzShejishi.html")
    public String torzShejishi(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "ruanzhuangsjs";
    }

    @RequestMapping("/tozcShejishi.html")
    public String tozcShejishi(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "zcShejishi.html";
    }

    @RequestMapping("/toSipaigushi.html")
    public String toSipaigushi(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "sipaigushi";
    }

    @RequestMapping("/toQiyewenhua.html")
    public String toQiyewenhua(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "qiyewenhua";
    }

    @RequestMapping("/toHezuohuoban.html")
    public String toHezuohuoban(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "hezuohuoban";
    }

    @RequestMapping("/toLianxiwomen.html")
    public String toLianxiwomen(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "lianxiwomen";
    }

    @RequestMapping("/bo/toAnliNav.html")
    public String toAnliNav(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());

        return "bo/boAnliNav";
    }

    @RequestMapping("/{zxStyle}/toAnli.html")
    public String toAnli(@PathVariable int zxStyle, HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        System.out.println(">>>>>zxStyle=" + zxStyle);
        request.setAttribute("zxStyle", zxStyle);
        return "zxiuanli";
    }

    @RequestMapping("/{anliId}/anliDetail.html")
    public String anliDetail(@PathVariable int anliId, HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        request.setAttribute("anliDetail", boService.getValueById(anliId));
        return "anliDetail";
    }

    @RequestMapping("/{sjsId}/sjsDetail.html")
    public String sjsDetail(@PathVariable int sjsId, HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        request.setAttribute("shejishiDetail", boService.loadShejishiDetail(sjsId));
        return "sjsDetail";
    }

    @RequestMapping("/bo/toModifyShejishi.html")
    public String toModifyShejishi(int sjsId, HttpServletRequest request) {
        request.setAttribute("sjsInfo", boService.getValue4Common(sjsId));
        request.setAttribute("picUrl", fileUrl);
        return "bo/boModifyShejishi";
    }

    @RequestMapping("/bo/toModifyAnli.html")
    public String toModifyAnli(int anliId, HttpServletRequest request) {
        request.setAttribute("anliInfo", boService.getValue4Common(anliId));
        request.setAttribute("picUrl", fileUrl);
        return "bo/boModifyAnli";
    }

    @RequestMapping("/toAllAnli.html")
    public String toAllAnli(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "allAnli";
    }

    @RequestMapping("/allShejishi.html")
    public String toAllShejishi(HttpServletRequest request) {
        request.setAttribute("navList", navigationService.listEnableKeyValue());
        request.getServletContext().setAttribute("webInfo", boService.getWebInfo());
        return "allShejishi";
    }

}
